package com.jackchen.web.control;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.NumberUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jackchen.common.i18n.I18n;
import com.jackchen.po.Dball;
import com.jackchen.po.Pager;
import com.jackchen.service.DballService;
import com.jackchen.util.StringUtil;
import com.jackchen.vo.DiaryVO;

@Controller
@RequestMapping(value="/lottery")
public class LotteryControl {

	@Autowired
	private DballService dballService;
	@Autowired
	private I18n i18n;
	
	private static final Logger log  = Logger.getLogger(LotteryControl.class);

	@RequestMapping(value="/dball")
	public String getPage(HttpServletRequest request,HttpServletResponse response){
		i18n.setLocalLanguage(request, response);
		return "lottery/dball/dballview";
	}

	@RequestMapping(value="/getAllDball")
	public @ResponseBody Map getAllDball(HttpServletRequest request,HttpServletResponse response){
		log.debug("URL:/dball/getAllDball.control  mapping successfully,now it's beginning to process...");
		//i18n.setLocalLanguage(request, response);
//		try{
//			log.info(request.getCharacterEncoding());
//			request.setCharacterEncoding("utf-8");
//		}catch(UnsupportedEncodingException e){
//			log.error("un-support encoding method!", e);
//		}catch(Exception e){
//			log.error("encoding transform failed!", e);
//		}
		
		String rows = request.getParameter("rows");//获得请求的每页记录数
		String page = request.getParameter("page");//获得请求的页码
		String startTime = request.getParameter("StartTime");
		String endTime = request.getParameter("EndTime");
		String operationType = request.getParameter("OperationType");
		log.debug("PageSize:"+rows+"PageNo:"+page);	
		Map map = new HashMap();
		try{
			int pageSize = Integer.parseInt(rows); 
			int pageNo = Integer.parseInt(page);
			if(pageSize>0&&pageNo>0){
				int startRow = pageSize*(pageNo-1);
				Map queryMap = dballService.queryDballOnPage(startTime, endTime, pageSize, startRow);
				Pager pager = (Pager)queryMap.get("pager");
				List<Dball> queryList= (List<Dball>)queryMap.get("querylist");
				if(null!=pager&&null!=queryList){
					map.put("totalPageNum", pager.getTotalPages());
					map.put("currentPageNo", pageNo);
					map.put("totalRecords", pager.getTotalRows());
					map.put("dataList", queryList);
					return map;
				}
			}
			return map;
		}catch(NumberFormatException e){
			e.printStackTrace();
		}catch(Exception e){
			e.printStackTrace();
		}
		return map;	
	}

	/**
	 * 注册新用户
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/addDball")
	public @ResponseBody boolean addNewDball(Dball dball){
//		try {
//			request.setCharacterEncoding("utf-8");
//		} catch (UnsupportedEncodingException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
		//get 方式需要用toGb方法转换, post方式不需要转换
		boolean result = dballService.createNewDball(dball);
		return result;
	}

	/**
	 * 修改日志
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/getEditDball")
	public @ResponseBody Map getEditDball(HttpServletRequest request, HttpServletResponse response){

		HttpSession session = request.getSession();
//		String userName = (String) session.getAttribute(LoginFilter.SESSION_USER_NAME);
				
		// 查询下拉框数据
		Map map = new HashMap();
		
		// 根据用户id查询用户
		String dballStrId = request.getParameter("dballid");
		Integer dballId = Integer.parseInt(dballStrId);
		if (dballId != null) {
			Dball dball = dballService.queryDballById(dballId);
			map.put("dball", dball);
		} else {
			map.put("dball", new Dball());
		}
		return map;
		
		
//		String username = toGb(request.getParameter("username"));
//		String realname = toGb(request.getParameter("realname"));
//		String content = toGb(request.getParameter("content"));
//		String langFlagStr = toGb(request.getParameter("language"));
//		
//		boolean result = resourceService.createNewDiary(username, realname, content);
//		return result;
	}	

	@RequestMapping(value="/updateDball")
	public @ResponseBody boolean editDball(Dball dball){
		
		//get 方式需要用toGb方法转换, post方式不需要转换
//		String diaryname = request.getParameter("diaryname");
//		String realname = request.getParameter("realname");
//		String content = request.getParameter("content");
//		String langFlagStr = request.getParameter("language");
		
		
		boolean result = dballService.updateDball(dball);
		return result;
	}	

	@RequestMapping(value="/deleteDball")
	public @ResponseBody String deleteDball(HttpServletRequest request, HttpServletResponse response){
		String idsStr = request.getParameter("id");
		String[] ids = idsStr.split(",");
		for(String id:ids){
			dballService.deleteById(Dball.class, Integer.parseInt(id));
		}
//		Object[] result = resourceService.delete(ids);
//		String type = result[0].toString();
		
		return "success";
	}
	
	/**
	 * 进入编辑用户页面入口
	 * 
	 * @return
	 */
	@RequestMapping(value="/openUpdateDball")
	public String openUpdateDball(HttpServletRequest request, HttpServletResponse response){
		return "/lottery/dball/editDball";
	}

	/**
	 * 进入添加新日记页面入口
	 * 
	 * @return
	 */
	@RequestMapping(value="/openAddDball")
	public String openAddDball(HttpServletRequest request, HttpServletResponse response){
		return "/lottery/dball/addDball";
	}	
}
